package com.pet.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.entity.Shop;
import com.pet.server.service.impl.ShopServiceImpl;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShopServlet", urlPatterns = "/shop")
public class ShopServlet extends HttpServlet {
  @Resource private ShopServiceImpl ssi;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String method = request.getParameter("method");
    if (method.equalsIgnoreCase("show")) {
      getAll(request, response);
    } else if (method.equalsIgnoreCase("showSome")) {
      getSomeShopsByPage(request, response);
    } else if (method.equalsIgnoreCase("detail")) {
      detail(request, response);
    } else if (method.equalsIgnoreCase("apply")) {
      apply(request, response);
    } else if (method.equalsIgnoreCase("update")) {
      update(request, response);
    } else if (method.equalsIgnoreCase("delete")) {
      delete(request, response);
    }
  }

  /**
   * 分页查询所有
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  protected void getAll(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    List<Shop> list = ssi.getAllByPage(pageNo, pageSize);
    ResultUtil<List> resultUtil = null;
    if (list != null) {
      resultUtil = new ResultUtil<>(list);
    } else {
      resultUtil = new ResultUtil<>("服务器异常");
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }

  /**
   * 分页条件查询
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  protected void getSomeShopsByPage(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    String shopName = request.getParameter("shopName");
    String licenseNumber = request.getParameter("licenseNumber");
    String address = request.getParameter("address");
    String juridicalPerson = request.getParameter("juridicalPerson");

    Shop shop = new Shop();
    shop.setShopName(shopName);
    shop.setShopAddress(address);
    shop.setLicenseNumber(licenseNumber);
    shop.setShopCorporation(juridicalPerson);
    List<Shop> list = ssi.getSomeShopsByPage(pageNo, pageSize, shop);
    ResultUtil<List> resultUtil = null;
    if (list != null) {
      resultUtil = new ResultUtil<>(list);
    } else {
      resultUtil = new ResultUtil<>("服务器异常");
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }

  /**
   * 查看详情
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  protected void detail(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("shopId"));
    Shop shop = ssi.detail(id);
    ResultUtil<Shop> resultUtil = null;
    if (shop != null) {
      resultUtil = new ResultUtil<>(shop);
    } else {
      resultUtil = new ResultUtil<>("服务器异常");
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }

  /**
   * 门店申请
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  protected void apply(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String shopName = request.getParameter("shopName");
    String licenseNumber = request.getParameter("licenseNumber");
    String licensePicture = request.getParameter("licensePicture");
    String address = request.getParameter("address");
    String location = request.getParameter("location");
    String juridicalPerson = request.getParameter("juridicalPerson");
    String phone = request.getParameter("phone");
    String headPicture = request.getParameter("headPicture");
    String characteristic = request.getParameter("characteristic");
    String commissionRate = request.getParameter("commissionRate");

    Shop shop = new Shop();
    shop.setShopName(shopName);
    ;
    shop.setLicenseNumber(licenseNumber);
    ;
    shop.setLicensePicture(licensePicture);
    shop.setShopAddress(address);
    shop.setShopLocation(location);
    shop.setShopCorporation(juridicalPerson);
    shop.setCorporationPhone(phone);
    shop.setCorporationAvatar(headPicture);
    shop.setShopCharacteristic(characteristic);
    shop.setCommissionRate(commissionRate);

    boolean result = ssi.apply(shop);
    ResultUtil<Boolean> resultUtil = new ResultUtil<>(result);
    ObjectMapper obj = new ObjectMapper();
    String json = obj.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }

  /** 门店修改 */
  protected void update(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("shopId"));
    String shopName = request.getParameter("shopName");
    String licenseNumber = request.getParameter("licenseNumber");
    String licensePicture = request.getParameter("licensePicture");
    String address = request.getParameter("address");
    String location = request.getParameter("location");
    String juridicalPerson = request.getParameter("juridicalPerson");
    String phone = request.getParameter("phone");
    String headPicture = request.getParameter("headPicture");
    String characteristic = request.getParameter("characteristic");
    String commissionRate = request.getParameter("commissionRate");

    Shop shop = new Shop();
    shop.setId(id);
    shop.setShopName(shopName);
    ;
    shop.setLicenseNumber(licenseNumber);
    ;
    shop.setLicensePicture(licensePicture);
    shop.setShopAddress(address);
    shop.setShopLocation(location);
    shop.setShopLocation(juridicalPerson);
    shop.setCorporationPhone(phone);
    shop.setCorporationAvatar(headPicture);
    shop.setShopCharacteristic(characteristic);
    shop.setCommissionRate(commissionRate);

    boolean result = ssi.update(shop);
    ResultUtil<Boolean> resultUtil = new ResultUtil<>(result);
    ObjectMapper obj = new ObjectMapper();
    String json = obj.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }

  /** 门店删除 */
  protected void delete(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("shopId"));
    boolean result = ssi.delete(id);
    ResultUtil<Boolean> resultUtil = new ResultUtil<>(result);
    ObjectMapper obj = new ObjectMapper();
    String json = obj.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }
}
